define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'statistics',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                pk:"key",
                sortName: 'updatetime',
                search:false,
                commonSearch: true,
                searchFormVisible: true,
                // 普通搜索拓展
                commonSearchExtra:[
                    {
                        name : 'group',
                        type : 'checkbox',
                        title: '聚合',
                        placeholder : '请选择',
                        options : {
                            'date': '日期',
                            'live_id': '现场',
                            'auth_id': '鉴权号',
                            'server_id': '公众号',
                            'partner_id':'合作方',
                            'admin_id':'商务'
                        },
                        value : ['date']               
                    }
                ],

                queryParams:function(params) {
                    // 要显示的字段
                    params.fields = '';
                    params.group = 'date';
                    // 要关联的拓展信息
                    params.extra = 'live,auth,server,partner,admin';
                    return params;
                },
                
                columns: [
                    [
                        {
                            field: 'date',
                            title: __('date'),
                            operate: 'BETWEEN',
                            addclass: 'datetimepicker',
                            data: 'data-date-format="YYYY-MM-DD"'
                        },
                        {
                            field: 'live.name',
                            title: __('live_name'),
                            align:'left',
                            operate:"ET",
                            selectPage:{
                                'source': '/live/admin.live/selectpage',
                                'searchTable': 'live/live',
                                'selectToCloseList': 'false'
                            },
                            name:"live_id",
                            visible: false,
                            placeholder:"请选择"
                        },
                        {
                            field: 'auth.name'
                            ,title: __('auth_name')
                            ,operate:"ET"
                            ,selectPage:{
                                'source': '/wechat/admin.auth/selectpage',
                                'searchTable': 'wechat/open_auth',
                                'selectToCloseList': 'false'
                            }
                            ,name:"auth_id"
                            ,visible: false
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'server.name'
                            ,title: __('server_name')
                            ,align:'left'
                            ,operate:"ET"
                            ,selectPage:{
                                'source': '/wechat/admin.server/selectpage',
                                'searchTable': 'wechat/open_server',
                                'selectToCloseList': 'false'
                            }
                            ,visible: false
                            ,name:"server_id"
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'partner.name'
                            ,title: __('partner_name')
                            ,align:'left'
                            ,operate:"ET"
                            ,selectPage:{
                                'source': '/partner/admin.partner/selectpage',
                                'searchTable': 'partner/partner',
                                'selectToCloseList': 'false'
                            }
                            ,visible: false
                            ,name:"partner_id"
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'admin.nickname'
                            ,title: __('admin_name')
                            ,align:'left'
                            ,operate:"ET"
                            ,selectPage:{
                                'source': '/admin/auth.admin/selectpage',
                                'searchTable': 'admin/admin',
                                'selectToCloseList': 'false',
                                'field':'nickname'
                            }
                            ,visible: false
                            ,name:"admin_id"
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'displays',
                            title: __('displays'),
                            operate: false
                        },
                        {
                            field: 'focus',
                            title: __('focus'),
                            operate: false
                        },
                        {
                            field: 'cancel',
                            title: __('cancel'),
                            operate: false
                        },
                        {
                            field: 'updatetime',
                            title: __("Update time"),
                            formatter: Table.api.formatter.datetime,
                            operate: false
                        }
                    ]
                ],
                onRefresh : function(params) {
                    var columns = {
                        date : 'date',
                        live_id : 'live.name',
                        auth_id : 'auth.name',
                        server_id:'server.name',
                        partner_id : 'partner.name',
                        admin_id : 'admin.nickname'
                    }

                    if(!params.hasOwnProperty('query')) return false;
                    var query = params.query;
                    var keys = [];
                    var vals = [];

                    for (var column in columns) {
                        keys.push(column);
                        vals.push(columns[column]);
                    }
                    groups = [];
                    if(query.hasOwnProperty('group')) {
                        var group = params.query.group;
                        groups = group ? group.split(',') : [];
                    }
                    $.each(keys, function(i, n) {
                        if(groups.length > 0 && $.inArray(n, groups) == -1) {
                            table.bootstrapTable('hideColumn', columns[n]);      
                        } else {
                           table.bootstrapTable('showColumn', columns[n]);
                        }
                    })
                    return false;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {}
    }
    return Controller;
});